/*
 * @Author: 詹真琦(legendryztachi@gmail.com)
 * @Date: 2021-12-16 20:39:06
 * @LastEditors: 詹真琦(legendryztachi@gmail.com)
 * @LastEditTime: 2022-04-06 17:00:11
 * @FilePath: \code\cwp-front\pc-collaboration\src\helper\relation-doc\index.js
 * @Description: 关联文档相关操作
 */
/**
 * @description: 生成文件唯一ID
 * @param {String}areaName 区域名称
 * @param {String}fileUrl 文件id
 */
export const createID = (areaName, fileUrl) => `${areaName}-${fileUrl}`;
/**
 * @description: 清除某个区域的所有文件
 * @param {string}areaName 区域名称
 */
export function clearArea(areaName) {
    //获取所有数据数组
    const list = fileUploadAttachments.values().toArray();
    list.forEach((d) => {
        //是当前区域的，就移除
        if (d.showArea === areaName) {
            fileUploadAttachments.remove(createID(areaName, d.fileUrl));
        }
    });
}

/**
 * @description: 添加传入列表文件
 * @param {Array}dataList 待添加文件列表
 * @param {string}areaName 区域名称
 */
export function addDataList(dataList, areaName) {
    dataList.forEach((d) => {
        fileUploadAttachments.put(createID(areaName, d.fileUrl), d);
    });
}

/**
 * @description: 整体替换某个区域文件。用于选择文件之后点击确定
 * @param {string}areaName 区域名称
 * @param {Array}dataList 数据
 */
export function replaceArea(areaName, dataList) {
    //先清除
    clearArea(areaName);
    //再添加
    addDataList(dataList, areaName);
}

/**
 * @description: 将普通对象转换成Attachment实例对象
 * @param {Object|Array} att一般的对象\或者数组
 * @param {String} areaName数据区域
 * @return {Attachment|Array}
 */
function transform(
    {
        id,
        type,
        filename,
        mimeType,
        createdate,
        size,
        fileUrl,
        description,
        extension,
        icon,
        iconFont,
        wpsOnlineEnable,
        officeTransformEnable,
        v,
        secretLevel,
        secretLevelName,
        canDel,
        createDate,
    },
    areaName
) {
    const attachment = new Attachment(
        id,
        '',
        '',
        '',
        +type,
        filename,
        mimeType,
        createdate,
        size,
        fileUrl,
        description,
        false,
        extension,
        icon,
        wpsOnlineEnable === 'true',
        officeTransformEnable,
        v
    );
    attachment.showArea = areaName;
    attachment.hasSaved = true;
    attachment.canFavourite = false;
    attachment.secretLevel = secretLevel;
    attachment.secretLevelName = secretLevelName;
    attachment.canDel = canDel;
    attachment.iconFont = iconFont;
    //V5-60630 【1/18更新】【公司协同】更新协同后，附件修改时间显示undefined 修改附件dialog老代码兼容写法
    createDate && (attachment.createDate = createDate);
    return attachment;
}
export function createAttachment(att, areaName) {
    //如果是数组
    if (Array.isArray(att)) {
        return att.map((d) => transform(d, areaName));
    } else {
        return transform(att, areaName);
    }
}
